/*
 * ModelControlLinker.cpp
 *
 *  Created on: Mar 24, 2011
 *      Author: Craig Wilson
 */

#include "../inc/ModelControlLinker.h"

	ModelControlLinker::ModelControlLinker(){

	}

	ModelControlLinker::~ModelControlLinker(){

	}

	void ModelControlLinker::loadGame(char *fileName){
		MoveHistory* loaded=fileManager.LoadGame(fileName);
		delete loaded;
	}

	void ModelControlLinker::saveGame(char *fileName){
		fileManager.SaveGame(fileName,board.getMoveHistory());
	}

	void ModelControlLinker::newGame(){
		board.reset();
	}

	Action ModelControlLinker::click(Position click){
		if (currentAction==NO_PIECE_SELECTED){	//Selecting a Piece
			selected= board.getPiece(click);
			if (selected==NULL || selected->getColor()!=board.currentTurn()){
				selected=NULL;
				return NO_PIECE_SELECTED;
			}
			return currentAction=PIECE_SELECTED;
		}
		else if(currentAction==PIECE_SELECTED){	//Moving a Selected Piece
			vector<Position> moves=board.getMoves(selected->getPosition());
			for(int i=0;i<moves.size();i++)	//Checks if click is a valid move
				if (click==moves[i]){
					board.movePiece(selected->getPosition(),click);
					selected=NULL;
					return PIECE_MOVED;
				}

			//If click is not a valid move checks if it selected a valid piece
			selected= board.getPiece(click);
				if (selected==NULL || selected->getColor()!=board.currentTurn()){
					selected=NULL;
					return NO_PIECE_SELECTED;
				}
			return currentAction=PIECE_SELECTED;
		}

	}

	bool ModelControlLinker::Undo(){
		return board.undo();

	}
	vector<Position> ModelControlLinker::getHighlighted(){
		return board.getMoves(selected->getPosition());
	}

	Board* ModelControlLinker::getBoard(){
		return &board;
	}




